import { $ } from './library/jquery-module.js';

let bool = [];

function right(node, word = '') {
    node.siblings('.red').removeClass('show');
    node.siblings('.green').addClass('show');
    node.siblings('.tips').html(word);
}

function wrong(node, word = '') {
    node.siblings('.green').removeClass('show');
    node.siblings('.red').addClass('show');
    node.siblings('.tips').html(word);
}

$('.form-group').on({
    blur: async function() {
        // 注册表单验证
        switch ($(this).attr('id')) {
            case 'inputphone':
                if (/^1[3|4|5|8][0-9]\d{8}$/.test($(this).val())) {
                    await $.ajax({
                        url: '../../interface/regtest.php?phone=' + $(this).val(), // 请求地址
                        type: 'GET', // 请求方式
                        dataType: 'json', // 接收的数据类型
                        success: function(data) {
                            return data;
                        }
                    }).then(val => {
                        if (val.has) {
                            wrong($('#inputphone'), val.msg);
                            bool[0] = false;
                        } else {
                            right($('#inputphone'), val.msg);
                            bool[0] = true;
                        }
                    });
                } else {
                    wrong($(this), '请输入正确的11位手机号码');
                    bool[0] = false;
                }
                break;
            case 'inputnotemsg':
                if ($(this).val() == 123456) {
                    right($(this));
                    bool[1] = true;
                } else {
                    wrong($(this), '请输入正确的6位动态码');
                    bool[1] = false;
                }
                break;
            case 'inputPassword':
                if (/[ ]/.test($(this).val())) {
                    wrong($(this), '密码不能包括空格')
                    bool[2] = false;
                } else if ($(this).val().length < 6) {
                    wrong($(this), '密码需要大于6位');
                    bool[2] = false;
                } else {
                    right($(this));
                    bool[2] = true;
                }
                $('#inputPassword2').trigger('blur');
                break;
            case 'inputPassword2':
                if ($('#inputPassword').val() === $(this).val() && $(this).val() != '') {
                    right($(this));
                    bool[3] = true;
                } else if ($(this).val() == '') {
                    wrong($(this), '密码不能为空');
                    bool[3] = false;
                } else {
                    wrong($(this), '两次输入的密码不一样');
                    bool[3] = false;
                }
                break;
        }
        // console.log(bool);
        if (bool.every(el => el == true)) {
            $('.form-group>.col-sm-10>.btn-warning').prop("disabled", false);
        } else {
            $('.form-group>.col-sm-10>.btn-warning').prop("disabled", true);
        }

    }
}, '.col-sm-10>input');